# Maintainer: Sébastien "Seblu" Luttringer
# Contributor: Carsten Feuls <archlinux@carstenfeuls.de>
# Contributor: Gilles Hamel <hamelg at laposte dot net>

pkgname=grafana
pkgver=10.2.2
pkgrel=1
pkgdesc='Gorgeous metric viz, dashboards & editors for Graphite, InfluxDB & OpenTSDB'
url='https://grafana.com/'
arch=('loong64' 'x86_64')
license=('AGPL' 'Apache')
depends=('glibc' 'freetype2' 'fontconfig' 'gsfonts')
makedepends=('git' 'go' 'npm' 'grunt-cli' 'python' 'nodejs-lts-hydrogen' 'yarn')
backup=('etc/grafana.ini')
source=("git+https://github.com/grafana/grafana.git#tag=v$pkgver"
        'grafana.service'
        'grafana.sysusers'
        'grafana.tmpfiles'
       )
install=$pkgname.install
sha512sums=('SKIP'
            '02a597225ee6ba8da9491a94daa33fe681846e2140e02fbb461ff7d39acd4cf99694dc03fb5f2c5ecfd180b09c44a41fe639b121499b4222b67ada57dc60d850'
            '38b46d953837a7afa5a654dfeef163b210d56cad57d937018531d00cd63b5341d6f9cd777299bdc7a994d0cb7df09b8157aad0f166e76ad47564e1ad6d5441d4'
            'c1724bfc7cbd6a406f17acb661efca0f1e57e2c30cc1841f4cb7ccfc420adc40cc61cb1c023d00444827b2c40e9caa4c2fefbfd503419848c74a0b455b2375ab')

prepare() {
  cd $pkgname
  # apply patch from the source array (should be a pacman feature)
  local filename
  for filename in "${source[@]}"; do
    if [[ "$filename" =~ \.patch$ ]]; then
      echo "Applying patch ${filename##*/}"
      patch -p1 -N -i "$srcdir/${filename##*/}"
    fi
  done
  # set arch linux paths
  sed -ri 's,^(\s*data\s*=).*,\1 /var/lib/grafana,' conf/defaults.ini
  sed -ri 's,^(\s*plugins\s*=).*,\1 /var/lib/grafana/plugins,' conf/defaults.ini
  sed -ri 's,^(\s*provisioning\s*=).*,\1 /var/lib/grafana/conf/provisioning,' conf/defaults.ini
  sed -ri 's,^(\s*logs\s*=).*,\1 /var/log/grafana,' conf/defaults.ini

  sed -i "s/v8.4.0-pre/v$pkgver/" package.json
}

build() {
  cd grafana
  echo 'Generate golang'
  make gen-go

  echo 'building the backend'
  export CGO_LDFLAGS="${LDFLAGS}"
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export GOFLAGS="-buildmode=pie -trimpath -modcacherw"
  go run build.go setup
  go run build.go build

  echo 'building the frontend'
  export NPM_CONFIG_PREFIX="$srcdir/npm"
  export PATH+=":$NPM_CONFIG_PREFIX/bin"
  export NODE_OPTIONS="--max-old-space-size=16000" # Increase to 8 GB
  yarn install || cat /tmp/*/build.log
  NODE_ENV=production yarn run build
}

package() {
  install -Dm644 grafana.tmpfiles "$pkgdir/usr/lib/tmpfiles.d/grafana.conf"
  install -Dm644 grafana.sysusers "$pkgdir/usr/lib/sysusers.d/grafana.conf"
  install -Dm644 grafana.service "$pkgdir/usr/lib/systemd/system/grafana.service"
  cd $pkgname
  install -Dsm755 bin/linux-amd64/grafana "$pkgdir/usr/bin/grafana"
  install -Dsm755 bin/linux-amd64/grafana-server "$pkgdir/usr/bin/grafana-server"
  install -Dsm755 bin/linux-amd64/grafana-cli "$pkgdir/usr/bin/grafana-cli"
  install -Dm640 -o207 -g207 conf/sample.ini "$pkgdir/etc/$pkgname.ini"
  install -Dm644 conf/defaults.ini "$pkgdir/usr/share/$pkgname/conf/defaults.ini"
  install -dm755 "$pkgdir/usr/share/grafana/"
  for i in public tools; do
    cp -r "$i" "$pkgdir/usr/share/grafana/$i"
  done

  # Remove unit tests
  rm -r "$pkgdir/usr/share/grafana/public/test"
}

# vim:set ts=2 sw=2 et:
